#/usr/bin/python
#coding=utf-8
import sys
reload(sys)
sys.path.append("..")
import unittest
import paramunittest
import readConfig as readConfig
from common import common
from common import businessCommon
from common import Log as Log
from common import configHttp as configHttp

uploadImg_xls = common.get_xls("userCase.xlsx", "updateImg")
localReadConfig = readConfig.ReadConfig()
localConfigHttp = configHttp.ConfigHttp()
info ={}

@paramunittest.parametrized(*uploadImg_xls)
class UploadImg(unittest.TestCase):

    def setParameters(self, case_name, method, token, file, result, code, msg):
        """
        set param
        :param case_name:
        :param method:
        :param token:
        :param file:
        :param result:
        :param code:
        :param msg:
        :return:
        """
        self.case_name = str(case_name)
        self.method = str(method)
        self.token = str(token)
        self.file = str(file)
        self.result = str(result)
        self.code = str(code)
        self.msg = str(msg)
        self.response = None
        self.info = None

    def description(self):
        """

        :return:
        """
        self.case_name

    def setUp(self):
        """

        :return:
        """
        self.log = Log.MyLog.get_log()
        self.logger = self.log.get_logger()
        self.login_token = businessCommon.login()

    def testUploadImg(self):
        """
        test body
        :return:
        """
        # set url
        self.url = common.get_url_from_xml('uploadImg')
        print "-----------------self.ulr",self.url
        localConfigHttp.set_url(self.url)

        # set header
        if self.token == '0':
            token = self.login_token
        elif self.token == '1':
            token = localReadConfig.get_headers("token_v")
        else:
            token = self.token
        data = {'token': token}
        localConfigHttp.set_data(data)

        # set files
        localConfigHttp.set_files(self.file)

        # set data
        #data = {'whole': self.whole}
        #localConfigHttp.set_data(data)

        # test interface
        self.response = localConfigHttp.postWithFile()

        # check result
        self.checkResult()

    def tearDown(self):
        """

        :return:
        """
        # logout
        businessCommon.logout(self.login_token)
        self.log.build_case_line(self.case_name, self.info['code'], self.info['desc'])

    def checkResult(self):
        """
        check test result
        :return:
        """
        self.info = self.response.json()

        common.show_return_msg(self.response)

        if self.result == '0':
            self.assertEqual(self.info['code'], self.code)
            self.assertEqual(self.info['desc'], self.msg)
            #self.assertIn('http://img.shein.com/', self.info['info']['imgUrl'])
        if self.result == '1':
            self.assertEqual(self.info['code'], self.code)
            self.assertEqual(self.info['desc'], self.msg)

